package com.hadron.media.slideshow.parser 
{
	import com.hadron.media.slideshow.data.SlideshowVars;
	import com.hadron.media.slideshow.items.ISlideshowItem;
	import com.hadron.media.slideshow.patterns.factory.ItemCreator;
	import com.hadron.media.slideshow.patterns.factory.TransitionCreator;
	import temple.data.collections.Collection;
	import temple.data.collections.HashMap;
	import temple.data.collections.ICollection;
	
	/**
	 * SlideshowXmlParser
	 * @author Hadron
	 * date created: 05-22-2011
	 */
	public class SlideshowXmlParser implements ISlideshowXMLParser 
	{
		///////////////////////////
		// FIELDS
		///////////////////////////
		public static const SETTINGS_NODE_NAME:String = "slideshowSettings";
		
		/** @private */
		private var _xml:XML;
		/** @private */
		private var _vars:SlideshowVars;
		/** @private */
		private var _items:ICollection = new Collection([]);
		/** @private */
		private var _itemsByName:HashMap = new HashMap("itemsByName");
		/** @private */
		private var _itemsByURL:HashMap = new HashMap("itemsByURL");
		/**
		  * This method constructs instance of this class
		 */
		public function SlideshowXmlParser() { }
		
		/*------------------- PUBLIC METHODS -------------------*/
		/* INTERFACE temple.data.xml.IXMLParsable */
		
		public function parseXML(xml:XML):Boolean 
		{
			_xml = xml;
			var result:Boolean = false;
			if (SETTINGS_NODE_NAME in xml) {
				//override slideshowVars settings
				var settings:SlideshowVars = new SlideshowVars();
				var node:XMLList = xml[SETTINGS_NODE_NAME];
				if ('@autoLoad' in node) settings.autoLoad(node.@autoLoad == "true" ? true : false);
				if ('@autoPlay' in node) settings.autoPlay(node.@autoPlay == "true" ? true : false);
				if ('@displayTime' in node) settings.displayTime(parseFloat(node.@displayTime));
				if ('@fadeTime' in node) settings.fadeTime(parseFloat(node.@fadeTime));
				if ('@background' in node) settings.background(node.@background == "true" ? true : false);
				//TODO: set the rest of settings
				_vars = settings;
			}
			
			createItems(xml.items[0]);
			return true;
		}
		
		/*------------------ GETTERS & SETTERS -----------------*/
		/* INTERFACE com.hadron.media.slideshow.parser.ISlideshowXMLParser */
		public function get vars():SlideshowVars { return _vars; }
		public function get xml():XML { return _xml; }
		public function get items():ICollection { return _items; }
		public function get itemsByName():HashMap { return _itemsByName; }
		public function get itemsByURL():HashMap { return _itemsByURL; }
		
		/*------------------- PRIVATE METHODS ------------------*/
		private function createItems(items:XML):void
		{
			//create items using factory itemCreator
			//TODO: create another hash map in order to keep track of references url = item
			var item:ISlideshowItem;
			var itemsList:XMLList = items.children();
			var node:XML;
			for (var i:int = 0; i < itemsList.length(); i++) 
			{
				node = itemsList[i];
				item = ItemCreator.getInstance().createItem(node.@url);
				item.transitionBehavior = TransitionCreator.instance.createBehavior(item,  node.@transition);
				item.transitionBehavior.vars = { fadeInTime:vars.vars.fadeTime };
				_items.addItem(item);
				_itemsByName[node.@name] = item;
				_itemsByURL[node.@url] = item;
			}	
		}
		/*------------------- EVENT HANDLERS --------------------*/
	}
}